package com.hly.musicbackground.model.music.rest;

import com.hly.musicbackground.Final.SongLinkFinal;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.model.music.VO.SongLinkVO;
import com.hly.musicbackground.model.music.params.SongLinkDelParams;
import com.hly.musicbackground.model.music.params.SongLinkEmitParams;
import com.hly.musicbackground.model.music.params.SongLinkListParams;
import com.hly.musicbackground.model.music.service.SongLinkService;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 喜欢歌曲、歌单歌曲
 * */
@RestController
@RequestMapping("/api/link")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = " 喜欢歌曲/歌单歌曲")
public class SongLinkController {

    private final SongLinkService songLinkService;
    private final SongService songService;

    /**
     * 获取(歌单、喜欢)所属的歌曲列表
     * @param page 页数
     * @param limit 数量
     * @param params
     * */
    @GetMapping("/list")
    @ApiOperation(value = "获取(歌单、喜欢)所属的歌曲列表")
    public Result<List<SongLinkVO>> list(SongLinkListParams params,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "20") int limit){
        List<SongLinkVO> musicListVO = songLinkService
                .getSongList(params.getType(), params.getLink(), page, limit);
        return Result.ok(musicListVO);
    }

    /**
     * 添加修改歌曲所属
     * @param params
     * */
    @PostMapping("/emit")
    @ApiOperation(value = "添加修改歌曲所属")
    public Result emit(@RequestBody SongLinkEmitParams params){
        songLinkService.emitSong(params);
        return Result.ok();
    }

    /**
     * 删除所属
     * @param params
     * */
    @PostMapping("/dels")
    @ApiOperation(value = "删除所属")
    public Result dels(@RequestBody SongLinkDelParams params){
        if (SongLinkFinal.LIKE.equals(params.getType()) && params.getId() == 0){
            params.setId(RequestContextUtils.getUser().getUserId());
        }
        songLinkService.dels(params.getId(), params.getRid(), params.getType());
        return Result.ok();
    }

}
